const user = require("../models/userDetail")
const records = require("../models/records")
const point = require("../models/point")
const question = require("../models/question")

var express = require('express');
var router = express.Router();

const formidable = require('formidable');

//返回图片
router.post('/', (req, res, next) => {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        if (err) {
            res.json({ 'state': false, 'msg': err })
        } else {
            user.find({
                flags: {
                    $all: fields.flags
                }
            }, {
                UUID: 1
            }, (err, docs) => {
                if (err) {
                    res.json({ 'state': false, 'msg': err });
                } else {
                    if (docs.length == 0) {
                        res.json({
                            state: true,
                            excercise_list: []
                        });
                    } else if (docs.length != 0) {
                        // console.log(docs)
                        let uuid_list = docs.map((ele) => {
                            return ele.UUID
                        })

                        records.find({
                            UUID: {
                                $in: uuid_list
                            }
                        }, (err, results) => {//获取对应标签学生的知识点id列表 、问题id列表
                            if (err) {
                                res.json({ 'state': false, 'msg': err })
                            } else {
                                let point_list = []
                                let question_list = []
                                for (let i = 0; i < results.length; i++) {
                                    if (results[i].attachment == true) point_list.push(results[i].point_ID)
                                    else if (results[i].attachment == false) question_list.push(results[i].question_ID)
                                }

                                var find_point = (point_list_para) => {
                                    return new Promise(function (resolve, reject) {
                                        point.find({
                                            _id: {
                                                $in: point_list_para
                                            }
                                        }, (err, docs) => {
                                            if (err) {
                                                res.json({ 'state': false, 'msg': err })
                                            } else {
                                                resolve({ question_list, docs })
                                            }
                                        })
                                    })

                                }

                                var find_question = ({ question_list: question_list_para, docs: point_list_result }) => {
                                    return new Promise(function (resolve, reject) {
                                        question.find({
                                            _id: {
                                                $in: question_list_para
                                            }
                                        }, (err, docs) => {
                                            if (err) {
                                                res.json({ 'state': false, 'msg': err })
                                            } else {
                                                resolve({ point_list_result, docs })
                                            }
                                        })
                                    })
                                }
                                

                                var return_data = ({ point_list_result, docs: question_list_result }) => {
                                    let point_list_temp = point_list_result.map((ele) => {
                                        return {
                                            level: "基本题",
                                            _id: ele._id,
                                            question: ele.practice.question_editor_Content,
                                            type: ele.practice.type,
                                            items: ele.practice.items,
                                            answer: ele.practice.answer,
                                            illustrationContent: ele.practice.illustrationContent,
                                            bloom: ele.bloom,
                                            answer_number: 0,
                                            right_answer_number: 0,
                                            right_percent: 0,
                                        }
                                    })
                                    
                                    let question_list_temp = question_list_result.map((ele) => {
                                        return {
                                            level: "提高题",
                                            _id: ele._id,
                                            question: ele.question,
                                            type: ele.type,
                                            items: ele.items,
                                            answer: ele.answer,
                                            illustrationContent: ele.illustrationContent,
                                            bloom: ele.bloom,
                                            answer_number: 0,
                                            right_answer_number: 0,
                                            right_percent: 0,
                                        }
                                    })

                                    for (let h = 0; h < results.length; h++) {
                                        if (results[h].attachment == true) {
                                            let point_list_temp_id = point_list_temp.map(ele => {
                                                return ele._id
                                            })
                                            let target_index = point_list_temp_id.findIndex(temp_id => {
                                                return temp_id == results[h].point_ID
                                            })
                                            if (target_index >= 0) {
                                                point_list_temp[target_index].answer_number++
                                                if (results[h].result == true) point_list_temp[target_index].right_answer_number++
                                            }
                                        } else if (results[h].attachment == false) {
                                            let question_list_temp_id = question_list_temp.map(ele => {
                                                return ele._id
                                            })
                                            let target_index = question_list_temp_id.findIndex(temp_id => {
                                                return temp_id == results[h].question_ID
                                            })
                                            if (target_index >= 0) {
                                                question_list_temp[target_index].answer_number++
                                                if (results[h].result == true) question_list_temp[target_index].right_answer_number++
                                            }


                                        }
                                    }

                                    question_list_temp.push.apply(question_list_temp, point_list_temp);
                                    question_list_temp.map((ele) => {
                                        if (ele.answer_number == 0) {
                                            ele.right_percent= ele.right_answer_number * 100 / ele.answer_number
                                        } else if (ele.answer_number != 0) {
                                            ele.right_percent= ele.right_answer_number * 100 / ele.answer_number
                                        }
                                    })
                                    
                                    let compare = property => {
                                        return function (a, b) {
                                            var value1 = a[property];
                                            var value2 = b[property];
                                            return value2 - value1;
                                        }
                                    }
                                    question_list_temp.sort(compare('right_percent'))

                                    res.json({
                                        state: true,
                                        excercise_list: question_list_temp
                                    });
                                }

                                find_point(point_list).then(find_question).then(return_data)


                            }
                        })
                    }

                }
            })
        }

    });

});

module.exports = router;